package com.ewanghuiju.app.ui.my.fragment;

import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseFragment;
import com.ewanghuiju.app.base.contract.my.WalletCenterContract;
import com.ewanghuiju.app.model.bean.local.WalletClassifyBean;
import com.ewanghuiju.app.model.bean.response.BillListResponBean;
import com.ewanghuiju.app.presenter.my.WalletCenterPresenter;
import com.ewanghuiju.app.ui.my.adapter.WalletBillListAdapter;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.TimeUtils;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.CustomPickerView;
import com.ewanghuiju.app.widget.popupwindow.WalletClassifyPopup;
import com.ewanghuiju.app.widget.popupwindow.WalletSelectedTimePopup;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import razerdp.basepopup.BasePopupWindow;
import razerdp.util.SimpleAnimationUtils;

/**
 * @author sj  qq:751943509
 * @class 钱包页面
 * @time 2020/2/28
 */
public class WalletCenterFragment extends BaseFragment<WalletCenterPresenter> implements WalletCenterContract.View {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_classify)
    TextView tvClassify;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_wallet_stream)
    TextView tvWalletStream;
    @BindView(R.id.layout_classify_main)
    RelativeLayout layoutClassifyMain;

    private int mType;

    private WalletBillListAdapter adapter;

    private OptionsPickerView pvCustomOptions;

    private TimePickerView pvCustomTime;

    private int currentPage = 1;

    private int currentStatus = 3;

    private String currentDate;

    private List<String> classfiyList;

    private List<WalletClassifyBean> mClassifyBeanList;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_walletcenter;
    }

    @Override
    protected void initEventAndData() {
        mType = getArguments().getInt(Constants.WALLETCENTER_TYPE);
        initRefreshLayout();
        initRecyleview();

        classfiyList = new ArrayList<>();
        mClassifyBeanList = new ArrayList<>();

        if (mType == 1) {
            classfiyList.add("拼团收益");
            classfiyList.add("拼团退款");
            classfiyList.add("充值明细");
            classfiyList.add("分红收益");

            WalletClassifyBean bean = new WalletClassifyBean();
            bean.setName("拼团收益");
            bean.setType(7);
            bean.setSelected(true);
            mClassifyBeanList.add(bean);

            bean = new WalletClassifyBean();
            bean.setName("拼团退款");
            bean.setType(6);
            bean.setSelected(false);
            mClassifyBeanList.add(bean);

            bean = new WalletClassifyBean();
            bean.setName("充值明细");
            bean.setType(4);
            bean.setSelected(false);
            mClassifyBeanList.add(bean);

            bean = new WalletClassifyBean();
            bean.setName("分红收益");
            bean.setType(9);
            bean.setSelected(false);
            mClassifyBeanList.add(bean);

        } else if (mType == 2) {
            classfiyList.add("拼团消费");
            classfiyList.add("提现明细");

            WalletClassifyBean bean = new WalletClassifyBean();
            bean.setName("拼团消费");
            bean.setType(8);
            bean.setSelected(true);
            mClassifyBeanList.add(bean);

            bean = new WalletClassifyBean();
            bean.setName("提现明细");
            bean.setType(5);
            bean.setSelected(false);
            mClassifyBeanList.add(bean);
        }

        currentDate = TimeUtils.date2String(TimeUtils.getNowDate(), "yyyy-MM-dd");
        tvClassify.setText(classfiyList.get(0));
        tvDate.setText(currentDate);
        LoadingDialogUtils.show(activity);
        if (mType == 1) {
            currentStatus = 7;
        } else {
            currentStatus = 8;
        }
        currentPage = 1;
        mPresenter.getData(currentPage, mType + "", currentDate, currentStatus + "");
    }


    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    public void initImmersionBar() {
//        super.initImmersionBar();
    }

    @Override
    public void showContent(BillListResponBean billListResponBean) {
        LoadingDialogUtils.dismissDialog_ios();
        if (billListResponBean == null) {
            return;
        }
        if (mType == 1) {
            tvWalletStream.setText("收入：" + billListResponBean.getSum());
        } else if (mType == 2) {
            tvWalletStream.setText("支出：" + billListResponBean.getSum());
        }
        List<BillListResponBean> list = billListResponBean.getList();
        if (list == null) {
            list = new ArrayList<>();
        }
        if (currentPage == 1) {
            refreshLayout.finishRefresh();
            adapter.setNewData(list);
        } else {
            if (list.size() < Constants.NUM_OF_PAGE_TWO) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore();
            }
            adapter.addData(list);
        }

//        if (adapter.getData().size() > 0) {
//            stateMain();
//        } else {
//            stateEmpty();
//        }
    }

    @Override
    public void showContentError() {
        LoadingDialogUtils.dismissDialog_ios();
        if (currentPage == 1) {
            refreshLayout.finishRefresh();
        } else {
            currentPage--;
            refreshLayout.finishLoadMore();
        }
//        if (adapter.getData().size() == 0) {
//            super.stateError();
//        }
    }

    @Override
    public void refreshData() {
        if (TextUtils.isEmpty(currentDate) || mPresenter == null) {
            return;
        }
        currentPage = 1;
        mPresenter.getData(currentPage, mType + "", currentDate, currentStatus + "");
    }

    private void initRefreshLayout() {
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(activity);
                currentPage++;
                mPresenter.getData(currentPage, mType + "", currentDate, currentStatus + "");
            }
        });
    }

    private void initRecyleview() {
        recyclerview.setNestedScrollingEnabled(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        recyclerview.setLayoutManager(linearLayoutManager);
        adapter = new WalletBillListAdapter(R.layout.item_wallet_bill_list, mType);
        adapter.setHasStableIds(true);
        recyclerview.setAdapter(adapter);
    }

    @OnClick({R.id.tv_classify, R.id.tv_date})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_classify:
                //分类
//                initClassify();
                initNewClassify();
                break;
            case R.id.tv_date:
                //选择时间
//                initTimePick();
                initNewTimePick();
                break;
            default:
                break;
        }
    }

    private WalletClassifyPopup walletClassifyPopup;

    /**
     * 新的分类样式
     */
    private void initNewClassify() {
        //分类
        setButtonRightIcon(tvClassify, true);
        if (walletClassifyPopup == null) {
            walletClassifyPopup = new WalletClassifyPopup(context, new WalletClassifyPopup.WalletClassPopupCallback() {

                @Override
                public void onClickCallback(int position) {
                    //分类点击事件
                    WalletClassifyBean walletClassifyBean = mClassifyBeanList.get(position);
                    tvClassify.setText(TextUtils.isEmpty(walletClassifyBean.getName()) ? "" : walletClassifyBean.getName());
                    //更新页面
                    LoadingDialogUtils.show(activity);
                    currentStatus = walletClassifyBean.getType();
                    currentPage = 1;
                    mPresenter.getData(currentPage, mType + "", currentDate, currentStatus + "");

                }
            });
        }

        walletClassifyPopup.setPopupGravity(Gravity.BOTTOM);
        walletClassifyPopup.setOutSideTouchable(true);
//        walletClassifyPopup.setOutSideDismiss(false);
//        walletClassifyPopup.setAllowInterceptTouchEvent(false);
//        walletClassifyPopup.setBlurBackgroundEnable(false);
        walletClassifyPopup.setAlignBackground(true);
        walletClassifyPopup.setAlignBackgroundGravity(Gravity.TOP);
        walletClassifyPopup.setWidth(walletClassifyPopup.getScreenWidth());
        walletClassifyPopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        walletClassifyPopup.setOnDismissListener(new BasePopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //关闭
                setButtonRightIcon(tvClassify, false);
            }
        });
        float fromX = 0;
        float fromY = -1f;
        float toX = 0;
        float toY = 0;
        Animation showAnimation = createTranslateAnimation(fromX, toX, fromY, toY);
//        Animation dismissAnimation = createTranslateAnimation(toX, fromX, toY, fromY);
        walletClassifyPopup.setShowAnimation(showAnimation);
//        walletClassifyPopup.setDismissAnimation(dismissAnimation);
        walletClassifyPopup.setDismissAnimation(SimpleAnimationUtils.getDefaultAlphaAnimation(false));
        walletClassifyPopup.setRecycleView(mClassifyBeanList);
        walletClassifyPopup.showPopupWindow(layoutClassifyMain);
    }

    private WalletSelectedTimePopup walletSelectedTimePopup;

    /**
     * 新的时间选择
     */
    private void initNewTimePick() {
        //时间选择器 ，自定义布局
        setButtonRightIcon(tvDate, true);
        if (walletSelectedTimePopup == null) {
            walletSelectedTimePopup = new WalletSelectedTimePopup(context, new WalletSelectedTimePopup.WalletSelectedTimePopupCallback() {
                @Override
                public void onSureClickCallback(String currentDate) {
                    tvDate.setText(currentDate);
                    LoadingDialogUtils.show(activity);
                    currentPage = 1;
                    mPresenter.getData(currentPage, mType + "", currentDate, currentStatus + "");
                }
            });
        }
        walletSelectedTimePopup.setPopupGravity(Gravity.BOTTOM);
        walletSelectedTimePopup.setOutSideTouchable(true);
        walletSelectedTimePopup.setBlurBackgroundEnable(false);
        walletSelectedTimePopup.setAlignBackground(true);
        walletSelectedTimePopup.setAlignBackgroundGravity(Gravity.TOP);
        walletSelectedTimePopup.setWidth(walletSelectedTimePopup.getScreenWidth());
        walletSelectedTimePopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        walletSelectedTimePopup.setOnDismissListener(new BasePopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //关闭
                setButtonRightIcon(tvDate, false);
            }
        });
        float fromX = 0;
        float fromY = -1f;
        float toX = 0;
        float toY = 0;
        Animation showAnimation = createTranslateAnimation(fromX, toX, fromY, toY);
//        Animation dismissAnimation = createTranslateAnimation(toX, fromX, toY, fromY);
        walletSelectedTimePopup.setShowAnimation(showAnimation);
//        walletSelectedTimePopup.setDismissAnimation(dismissAnimation);
        walletSelectedTimePopup.setDismissAnimation(SimpleAnimationUtils.getDefaultAlphaAnimation(false));
        walletSelectedTimePopup.setCustomPickerView();
        walletSelectedTimePopup.showPopupWindow(layoutClassifyMain);
    }

    /**
     * 切换右边图标
     *
     * @param isShowUp 是否显示
     */
    public void setButtonRightIcon(TextView textView, boolean isShowUp) {
        Drawable drawable = isShowUp ? ContextCompat.getDrawable(context, R.mipmap.screen_down_selected) : ContextCompat.getDrawable(context, R.mipmap.screen_down);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                drawable.getMinimumHeight());
        textView.setCompoundDrawables(null, null, drawable, null);
    }

    /**
     * 分类
     */
    private void initClassify() {
        if (pvCustomOptions != null) {
            pvCustomOptions.show();
            return;
        }
        pvCustomOptions = new CustomPickerView().getCustomOptions(context, new CustomPickerView.OnOptionsSureListener() {
            @Override
            public void onOptionsSureSelect(int options1, int options2, int options3, View v) {
                tvClassify.setText(classfiyList.get(options1));
                //更新页面
                LoadingDialogUtils.show(activity);
                if (mType == 1) {
                    if (options1 == 0) {
                        currentStatus = 7;
                    } else if (options1 == 1) {
                        currentStatus = 6;
                    } else if (options1 == 2) {
                        currentStatus = 4;
                    }
                } else if (mType == 2) {
                    if (options1 == 0) {
                        currentStatus = 8;
                    } else if (options1 == 1) {
                        currentStatus = 5;
                    }
                }
                currentPage = 1;
                mPresenter.getData(currentPage, mType + "", currentDate, currentStatus + "");
            }

            @Override
            public void onTimeSureSelect(Date date, View v) {

            }

            @Override
            public void onSureOrCancel() {

            }
        });
        pvCustomOptions.setPicker(classfiyList);//添加数据
        pvCustomOptions.show();
    }

    private void initTimePick() {
        //时间选择器 ，自定义布局
        if (pvCustomTime != null) {
            pvCustomTime.show();
            return;
        }
        //正确设置方式 原因：注意事项有说明
//        Calendar calendar = Calendar.getInstance();
//        Calendar startDate = Calendar.getInstance();
//        Calendar endDate = Calendar.getInstance();
//        int year = calendar.get(Calendar.YEAR);//当前年
//        int month = calendar.get(Calendar.MONTH) + 1;
//        startDate.set(2015, 0, 1);
//        endDate.set(year, month, 20);
        pvCustomTime = new CustomPickerView().getTimePick(context, new boolean[]{true, true, false, false, false, false}, new CustomPickerView.OnOptionsSureListener() {
            @Override
            public void onOptionsSureSelect(int options1, int options2, int options3, View v) {

            }

            @Override
            public void onTimeSureSelect(Date date, View v) {
                currentDate = TimeUtils.date2String(date, "yyyy-MM");
                tvDate.setText(currentDate);
                LoadingDialogUtils.show(activity);
                currentPage = 1;
                mPresenter.getData(currentPage, mType + "", currentDate, currentStatus + "");
            }

            @Override
            public void onSureOrCancel() {

            }
        });
        pvCustomTime.show();
    }


}
